<%@ page import="java.io.* , java.util.*, java.sql.*, com.myapp.struts.dao.*,com.myapp.struts.actionform.*,com.myapp.struts.beans.*, com.myapp.struts.servlets.*"%>
<html xmlns="http://www.w3.org/1999/xhtml">
    <!-- DW6 -->
    <head>
        <!-- Copyright 2005 Macromedia, Inc. All rights reserved. -->
        <title>Item Master</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <link href="../../../../css/cms.css" rel="stylesheet" type="text/css">       
        <script language="javascript">
            function tabEvent(eve, ccount){
                var count = document.getElementById("optionCount").value;
                var keynum;
                if(window.event) // IE
                    keynum = eve.keyCode;
                else if(eve.which) // Netscape/Firefox/Opera
                    keynum = eve.which;
                if(keynum == 9 && ccount == count){
                    count = parseInt(count) + 1;
                    document.getElementById("optionCount").value = count;
                    var appendStr = '<br><input type="text" name="icode" id="icode'+count+'"><input type="text" name="iname" id="iname'+count+'" /><input type="text" name="qty" id="qty'+count+'" /><select name="uom" id="uom'+count+'"></select><input type="text" name="rate" id="rate'+count+'" onKeyDown="javascript:tabEvent(event, '+count+');">';
                    document.getElementById('rrows').innerHTML = document.getElementById('rrows').innerHTML + appendStr;
                    var eleIds = "uom"+count;
                    fillOptions(eleIds);
                }
            }

            function fillOptions(eleId)
            {
                var elemnt = document.getElementById(eleId);
                var actualEle = document.getElementById("uom");
                for(i=0;i<actualEle.length;i++){
                    addOption(elemnt,actualEle[i].text,actualEle[i].text);
                }
            }

            function addOption(selectbox,text,value )
            {
                var optn = document.createElement("OPTION");
                optn.text = text;
                optn.value = value;
                selectbox.options.add(optn);
            }
            //-----------------------------
            function getXMLObject()  //XML OBJECT
            {
                var xmlHttp = false;
                try {
                    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
                }
                catch (e) {
                    try {
                        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
                    }
                    catch (e2) {
                        xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
                    }
                }
                if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
                    xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
                }
                return xmlHttp;  // Mandatory Statement returning the ajax object created
            }

            var xmlhttp = new getXMLObject();	//xmlhttp holds the ajax object

            function getItemDetails() {
                var sel = document.getElementById("itype");
                var opt = sel.options[sel.selectedIndex];
                var d = opt.value || opt.text;

                if(xmlhttp) {
                    xmlhttp.open("GET","../../../../AJAXGetItems?itype=" +d,true);
                    xmlhttp.onreadystatechange  = handleServerResponse;
                    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                    xmlhttp.send(null);
                }
            }

            function handleServerResponse() {
                if (xmlhttp.readyState == 4) {
                    if(xmlhttp.status == 200) {
                        document.getElementById('Group').options.length = 0;
                        var elemnt = document.getElementById("Group");
                        var textToSplit = xmlhttp.responseText;
                        var returnElements=textToSplit.split("|");
                        var valueLabelPair=null;
                        alert(returnElements);
                        
                    }else {
                        alert("Error during AJAX call. Please try again");
                    }
                }
            }
            //-----------------------------
			
        </script>                  
        <style type="text/css">
            <!--
            .style1 {color: #D3F1D5}
            -->
        </style>
    </head>
    <body>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td height="550" align="center" valign="top" >&nbsp;
                    <p>&nbsp;</p>

                    <form id="form" name="InsertItem" method="post" action="../../../../InsertItems">
                        <table width="674" border="0" align="center" cellpadding="0" cellspacing="0"
                               style="border-right: #000000 1px solid; border-top: #000000 1px solid; border-left: #000000 1px solid;  border-bottom: #000000 1px solid; font-size: 9pt; color: #000000; font-family: Arial;">
                            <tr>
                                <th height="30" colspan="5" align="center" valign="middle" ><span class="style13">Adding Items</span></th>
                            </tr>
                            <tr>
                                <td height="26" colspan="2" align="center" valign="middle"><strong>Material Type</strong></td>
                                <td colspan="3" align="center" valign="middle" >
                                    <select name='itype' id="itype" onChange="javascript:getItemDetails()">
                                        <option selected="selected"></option>
                                        <%
                    MaterialMasterDao imdao = new MaterialMasterDao();
                    Vector gp = imdao.getTypes();
                    if (gp != null) {
                        System.out.println("IN..................." + gp.size());
                        Iterator itg = gp.iterator();
                        while (itg.hasNext()) {
                            MaterialTypeFormBean gb = (MaterialTypeFormBean) itg.next();
                            String pname = gb.getMaterialType();
                                        %>
                                        <option value="<%=pname%>"><%=pname%></option>
                                        <%}

                    }%>
                                    </select>                          </tr>
                            <tr align="center" valign="middle" >
                                <th width="144" height="27" ><span class="style17">Item code</span></th>
                                <th width="148" ><span class="style5 style18">Item Name </span></th>
                                <th width="144" ><span class="style5 style18">Quantity</span></th>
                                <th width="69" ><a href="uomMaster.jsp" target="_blank" class="style1">U.O.M</a></th>
                                <th width="167" ><span class="style5 style18">Rate</span></th>
                            </tr>
                            <tr >
                                <td colspan="5" align="center" valign="middle" >
                                    <div id="rrows"><input type="hidden" name="optionCount" id="optionCount" ><input type="text" name="icode" id="icode"><input type="text" name="iname" id="iname" /><input type="text" name="qty" id="qty" /><select name='uom' id="uom"><option value="-1"></option><%UomDao uomdao = new UomDao(); Vector gp1 = uomdao.getAllUoms();if (gp1 != null) {Iterator itg = gp1.iterator();while (itg.hasNext()) {UomBean gb = (UomBean) itg.next();String pname = gb.getUom();%><option value="<%=pname%>"><%=pname%></option><%}}%></select><input type="text" name="rate" id="rate" onKeyDown="javascript:tabEvent(event, 0);"></div></td>
                            </tr>
                            <tr>
                                <td height="26" colspan="5" align="center" valign="middle"><label>
                                        <input type="submit" name="button" id="button" value="Add Item">
                                    </label></td>
                            </tr>
                        </table>
                    </form>
                    <script language="javascript">
                        document.getElementById("optionCount").value=0;
                    </script></td>
            </tr>
        </table>        
    </body>
</html>
